package com.hyb.hybmodulesblog.controller.front;

import com.hyb.hybmodulesblog.domain.Article;
import com.hyb.hybmodulesblog.domain.Comment;
import com.hyb.hybmodulesblog.service.IArticleService;
import com.hyb.hybmodulesblog.service.ICommentService;
import com.ruoyi.common.core.constant.CommentConstants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteSearchService;
import com.ruoyi.system.api.domain.ArticleResponseVo;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/front/comment")
public class FrontCommentController extends BaseController {

    @Autowired
    ICommentService commentService;

    @Autowired
    IArticleService articleService;

    @Autowired
    RemoteSearchService remoteSearchService;

    /*
    * 查询一级评论
    * */
    @GetMapping("/list")
    public TableDataInfo list(Comment comment){
        comment.setLevel(CommentConstants.LEVEL1);
        startPage();
        List<Comment> list = commentService.selectCommentList(comment);

        return getDataTable(list);
    }

    /*
    * 查询某个评论的二级评论
    * */
    @GetMapping("/listSecond")
    public TableDataInfo listSecond(Comment comment){
        comment.setLevel(CommentConstants.LEVEL2);
        comment.setParentId(comment.getId());
        startPage();
        List<Comment> list = commentService.selectCommentList(comment);
        return getDataTable(list);
    }

    /*
    * 评论
    * */
    @PostMapping("/send")
    public AjaxResult send(@RequestBody Comment comment){
        if (StringUtils.isEmpty(comment.getContent())){
            return AjaxResult.error("评论内容不能为空");
        }
        //文章是否存在
        Long articleId = comment.getArticleId();
        Article article = articleService.selectArticleById(articleId);
        if (article==null){
            return AjaxResult.error("文章不存在");
        }
        //获取用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return AjaxResult.error("获取用户信息失败");
        }
        SysUser sysUser = loginUser.getSysUser();
        comment.setUserId(sysUser.getUserId());
        comment.setUserAvatar(sysUser.getAvatar());
        comment.setUserNickName(sysUser.getNickName());
        Date nowDate = DateUtils.getNowDate();
        comment.setCommentTime(nowDate);
        comment.setCommentTime(nowDate);
        comment.setUpdateTime(nowDate);
        commentService.insertComment(comment);
        //更新评论数量
        Article at = new Article();
        at.setId(articleId);
        at.setCommenCount(article.getCommenCount()+1);

        articleService.updateArticle(at);
        //更新elasticSearch
        ArticleResponseVo articleResponseVo = new ArticleResponseVo();
        articleResponseVo.setId(articleId);
        articleResponseVo.setCommenCount(article.getCommenCount()+1);
        remoteSearchService.update(articleResponseVo, SecurityConstants.INNER);

        return AjaxResult.success();
    }

}
